package com.dg.river.core.util;

import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.provider.MediaStore;

import com.luck.picture.lib.tools.PictureFileUtils;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by hy on 2019/4/19
 * Description :
 */
public class BitmapUtils {

    public interface HttpCallBackListener {
        void onFinish(Bitmap bitmap);

        void onError(Exception e);
    }

    public Bitmap createBitmapThumbnail(Bitmap bitmap, boolean needRecycler) {
        try {
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            int newWidth = 80;
            int newHeight = 80;
            float scaleWidth = ((float) newWidth) / width;
            float scaleHeight = ((float) newHeight) / height;
            Matrix matrix = new Matrix();
            matrix.postScale(scaleWidth, scaleHeight);
            Bitmap newBitMap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
            if (needRecycler) bitmap.recycle();
            return newBitMap;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * bitmap转换
     *
     * @param
     * @return
     */
    public void getImage(final String path, final HttpCallBackListener listener) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                URL imageUrl = null;
                try {
                    imageUrl = new URL(path);
                } catch (MalformedURLException | NullPointerException e) {
                    e.printStackTrace();
                }
                if (imageUrl != null) {
                    try {
                        HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
                        conn.setDoInput(true);
                        conn.connect();
                        InputStream is = conn.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(is);
                        Bitmap bitmap1 = createBitmapThumbnail(bitmap, false);
                        if (listener != null) {
                            listener.onFinish(bitmap1);
                        }
                        is.close();
                    } catch (IOException e) {
                        if (listener != null) {
                            listener.onError(e);
                        }
                        e.printStackTrace();
                    }
                } else {
                    if (listener != null) {
                        listener.onFinish(null);
                    }
                }
            }

        }).start();

    }

    /**
     * 获取图片的旋转角度
     *
     * @param path
     * @return
     */
    public static int readPictureDegree(String path) {
        short degree = 0;
        try {
            ExifInterface exifInterface = new ExifInterface(path);
            int orientation = exifInterface.getAttributeInt("Orientation", 1);
            switch (orientation) {
                case 3:
                    degree = 180;
                    break;
                case 6:
                    degree = 90;
                    break;
                case 8:
                    degree = 270;
            }
        } catch (IOException var4) {
            var4.printStackTrace();
        }

        return degree;
    }

    /**
     * 旋转图片
     *
     * @param degree
     * @param file
     */
    public static void rotateImage(int degree, File file) {
        if (degree > 0) {
            try {
                android.graphics.BitmapFactory.Options opts = new android.graphics.BitmapFactory.Options();
                opts.inSampleSize = 2;
                Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), opts);
                Bitmap bmp = com.luck.picture.lib.tools.BitmapUtils.rotatingImage(bitmap, degree);
                BufferedOutputStream bos = null;
                try {
                    bos = new BufferedOutputStream(new FileOutputStream(file));
                    bmp.compress(Bitmap.CompressFormat.JPEG, 80, bos);
                    bos.flush();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    PictureFileUtils.close(bos);
                }
            } catch (Exception var6) {
                var6.printStackTrace();
            }
        }
    }

    /**
     * content uri 转 真实路径
     *
     * @param selectedVideoUri
     * @param contentResolver
     * @return
     */
    public static String getFilePathFromContentUri(Uri selectedVideoUri, ContentResolver contentResolver) {
        String filePath;
        String[] filePathColumn = {MediaStore.MediaColumns.DATA};
        Cursor cursor = contentResolver.query(selectedVideoUri, filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        filePath = cursor.getString(columnIndex);
        cursor.close();
        return filePath;
    }
}
